import scrapy
from mooc.items import MoocItem


class SchoolSpider(scrapy.Spider):
    name = 'school'
    # allowed_domains = ['www.icourse163.org']
    start_urls = ['https://www.icourse163.org/university/view/all.htm#/']


    def parse_detail(self,response):
        # 接收首页传递过来额
        item = response.meta['item']
        item['content'] = response.xpath('//*[@id="g-body"]/div[2]/div[2]/div[2]/div/p/text()').extract_first()
        print(item)
        yield item


    def parse(self, response):
        li_list = response.xpath('//*[@id="g-body"]/div/div[2]/div[2]/a')
        for li in li_list:
            item = MoocItem()
            detail_url = 'https://www.icourse163.org' + li.xpath('./@href').extract_first()
            name = li.xpath('./img/@alt').extract_first()
            src = li.xpath('./img/@src').extract_first()
            item['name'] = name
            item['detail_url'] = detail_url
            item['src'] = src
            # 对详情页发请求获取页面源码数据
            # 手动请求的发送
            # 请求传参： mets = {},可以将该字典传递给对应的回调函数
            yield scrapy.Request(url=detail_url, callback=self.parse_detail, meta={'item': item})

